package com.liao.controller;

import com.liao.bean.Log;
import com.liao.bean.Order;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.CodeService;
import com.liao.service.LogService;
import com.liao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    @ResponseBody
    @RequestMapping("/queryLogListByPage")
    public PageResult<Log> queryCodeListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(defaultValue = "") String orderId) throws Exception {
        PageResult<Log> pageResult = logService.queryLogListByPage(currentPage,pageSize,orderId);
        return pageResult;
    }


    @ResponseBody
    @RequestMapping("/addLog")
    public Result addLog(@RequestBody Log log) throws Exception {

        logService.addLog(log);
        return new Result(true, Code.LOG_ADD_SUCCESS, Msg.LOG_ADD_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("/editLog")
    public Result editLog( @RequestBody Log log) throws Exception {
        logService.editLog(log);
        return new Result(true,Code.LOG_EDIT_SUCCESS, Msg.LOG_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteLog")
    public Result deleteLog(String logId) throws Exception {
        logService.deleteLog(logId);
        return new Result(true,Code.LOG_DELETE_SUCCESS, Msg.LOG_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteLogIds")
    public Result deleteLogIds(@RequestParam("logIds[]") List<String> logIds) throws Exception {
        for (String logId : logIds) {
            try {
                logService.deleteLog(logId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.LOG_DELETE_FAIL, Msg.LOG_DELETE_FAIL);
            }

        }
        return new Result(true,Code.LOG_DELETE_SUCCESS, Msg.LOG_DELETE_SUCCESS);
    }
}
